package zl.Controller;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import zl.Entity.Animal;
import zl.Entity.User;
import zl.Service.AnimalService;
import zl.Service.UserService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

@RestController
public class LoginController {
    @Autowired
    UserService userService;

    @Autowired
    AnimalService animalService;

    @RequestMapping("/login")
    public void loginCheck(@RequestParam("uname") String uname,
                           @RequestParam("pwd") String password,
                           HttpServletResponse resp) throws IOException {
        System.out.println("uanme"+uname);
        System.out.println("password"+password);
        User user=userService.selectUserByuser(new User(uname,password));
        if(user!=null){
            resp.getWriter().write("yes");
        }
        else
        {
            resp.getWriter().write("no");
        }
    }

    @RequestMapping(value = "/selectAll",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String selectAll(@RequestParam("page") int page,@RequestParam("limit") int limit) throws UnsupportedEncodingException {
        List<Animal> list=animalService.selectAll();
        List<String> list1=new LinkedList<>();
        JSONObject result = new JSONObject();
        String string="";
        for(int i=(page-1)*limit;i<limit+(page-1)*limit && i<list.size();i++){
            JSONObject a = new JSONObject();
            a.put("AId",list.get(i).getAId());
            a.put("name",list.get(i).getName());
            a.put("age",list.get(i).getAge());
            a.put("sex",list.get(i).getSex());
            a.put("species",list.get(i).getSpecies());
            a.put("introduction",list.get(i).getIntroduction());
            list1.add(a.toString());
        }
//        for(Animal animal:list){
//            JSONObject a = new JSONObject();
//            a.put("AId",animal.getAId());
//            a.put("name",animal.getName());
//            a.put("age",animal.getAge());
//            a.put("sex",animal.getSex());
//            a.put("species",animal.getSpecies());
//            a.put("introduction",animal.getIntroduction());
//            list1.add(a.toString());
//        }
        result.put("code",0);
        result.put("msg","");
        result.put("count",1000);
        result.put("data",list1);
        System.out.println(result.toString());
        return result.toString();
    }

    @RequestMapping("/edit")
    public void edit(Animal animal,HttpServletResponse response) throws IOException {
        Animal animal1=selectByAId(animal.getAId());
        animal.setId(animal1.getId());
        animalService.editUpdate(animal);
        response.getWriter().write("yes");
    }

    @RequestMapping("/delete")
    public void delete(String AId,HttpServletResponse response) throws IOException {
        animalService.deleteById(AId);
        response.getWriter().write("yes");
    }

    public Animal selectByAId(String AId){
       return animalService.selectByAId(AId);
    }

    @RequestMapping("/save")
    public void save(Animal animal,HttpServletResponse response) throws IOException {
        if(animalService.selectByAId(animal.getAId())==null){
            animalService.save(animal);
            response.getWriter().write("yes");
        }
        else {
            response.getWriter().write("no");
        }
    }

    @RequestMapping("/deletes")
    public void deletes(@RequestParam("AIds") String []AIds,HttpServletResponse response) throws IOException {
        if(AIds.length>0){
            for(int i=0;i<AIds.length;i++){
                animalService.deleteById(AIds[i]);
            }
            response.getWriter().write("yes");
        }
        else {
            response.getWriter().write("no");
        }

    }
}
